# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2017 Patrick Griffis
# SPDX-FileCopyrightText: 2017 Danny Forghieri
# SPDX-FileCopyrightText: 2017 Nirbheek Chauhan
# SPDX-FileCopyrightText: 2017, 2021, 2022 Emmanuele Bassi
# SPDX-FileCopyrightText: 2018 Mathieu Duponchelle
# SPDX-FileCopyrightText: 2018, 2019, 2020 Christoph Reiter
# SPDX-FileCopyrightText: 2018 Kai Kang
# SPDX-FileCopyrightText: 2018 Carlos Garnacho
# SPDX-FileCopyrightText: 2018, 2019, 2020 Alexander Kanavin
# SPDX-FileCopyrightText: 2019, 2020 Chun-wei Fan
# SPDX-FileCopyrightText: 2019 Aaron Boxer
# SPDX-FileCopyrightText: 2019 Thibault Saunier
# SPDX-FileCopyrightText: 2019 Joshua Watt
# SPDX-FileCopyrightText: 2020 Xavier Claessens
# SPDX-FileCopyrightText: 2020 Philip Chimento
# SPDX-FileCopyrightText: 2021 John Ericson
# SPDX-FileCopyrightText: 2021 Cimbali
# SPDX-FileCopyrightText: 2021, 2023 Simon McVittie
# SPDX-FileCopyrightText: 2022 Andoni Morales Alastruey

subdir('cmph')

gir_dir_prefix = get_option('gir_dir_prefix')
if gir_dir_prefix == '' or gir_dir_prefix == get_option('datadir')
  gir_dir_prefix = get_option('datadir')
  gir_dir_pc_prefix = '${datadir}'
else
  gir_dir_pc_prefix = join_paths('${prefix}', gir_dir_prefix)
endif

glib_girdir = get_option('prefix') / gir_dir_prefix / 'gir-1.0'

gir_includedir = glib_includedir / 'girepository'

gi_visibility_h = custom_target(
  output: 'gi-visibility.h',
  command: [gen_visibility_macros, meson.project_version(), 'visibility-macros', 'GI', '@OUTPUT@'],
  install: true,
  install_dir: gir_includedir,
  install_tag: 'devel',
)

girepo_headers = files(
  'giarginfo.h',
  'gibaseinfo.h',
  'giboxedinfo.h',
  'gicallableinfo.h',
  'gicallbackinfo.h',
  'giconstantinfo.h',
  'gienuminfo.h',
  'gifieldinfo.h',
  'giflagsinfo.h',
  'gifunctioninfo.h',
  'giinterfaceinfo.h',
  'giobjectinfo.h',
  'gipropertyinfo.h',
  'giregisteredtypeinfo.h',
  'girepository.h',
  'gisignalinfo.h',
  'gistructinfo.h',
  'gitypeinfo.h',
  'gitypelib.h',
  'gitypes.h',
  'giunioninfo.h',
  'giunresolvedinfo.h',
  'givalueinfo.h',
  'givfuncinfo.h',
)

girepo_ffi_headers = [
  'girffi.h',
]

girepo_ffi_sources = files(
  'girffi.c',
)

install_headers(girepo_headers + girepo_ffi_headers, install_dir: gir_includedir)

gir_c_args = [
  '-DGI_COMPILATION',
  '-DG_LOG_DOMAIN="GLib-GIRepository"',
  '-DGIR_SUFFIX="gir-1.0"',
  '-DGIR_DIR="@0@"'.format(glib_girdir),
  '-DGOBJECT_INTROSPECTION_LIBDIR="@0@"'.format(glib_libdir),
  '-DGOBJECT_INTROSPECTION_DATADIR="@0@"'.format(glib_datadir),
]

custom_c_args = []

if cc.get_id() != 'msvc'
  custom_c_args = cc.get_supported_arguments([
    '-Wno-old-style-definition',
    '-Wno-cast-align',
  ])
endif

girepo_gthash_lib = static_library('girepository-gthash',
  sources: ['gthash.c', gi_visibility_h],
  include_directories : [configinc, girepoinc],
  c_args: gir_c_args + custom_c_args,
  dependencies: [
    cmph_dep,
    libglib_dep,
    libgmodule_dep,
    libgobject_dep,
  ],
  gnu_symbol_visibility : 'hidden',
)

girepo_gthash_dep = declare_dependency(
  link_with: girepo_gthash_lib,
  dependencies: [libglib_dep, libgmodule_dep, libgobject_dep],
  include_directories: [girepoinc],
)

if cc.get_id() != 'msvc'
  custom_c_args = cc.get_supported_arguments([
    '-Wno-unused-parameter',
    '-Wno-duplicated-branches',
    '-Wno-cast-align',
  ])
endif

libgirepository_internals = static_library('girepository-internals',
  sources: [
    'girmodule.c',
    'girnode.c',
    'giroffsets.c',
    'girparser.c',
    'girwriter.c',
    gi_visibility_h,
  ],
  c_args: gir_c_args + custom_c_args,
  gnu_symbol_visibility : 'hidden',
  include_directories : [configinc, girepoinc],
  dependencies: [girepo_gthash_dep, libffi_dep],
)

libgirepository_internals_dep = declare_dependency(
  link_with: libgirepository_internals,
  dependencies: libffi_dep,
  include_directories: [girepoinc],
)

girepo_sources = files(
  'gdump.c',
  'giarginfo.c',
  'gibaseinfo.c',
  'giboxedinfo.c',
  'gicallableinfo.c',
  'gicallbackinfo.c',
  'giconstantinfo.c',
  'gienuminfo.c',
  'gifieldinfo.c',
  'giflagsinfo.c',
  'gifunctioninfo.c',
  'ginvoke.c',
  'giinterfaceinfo.c',
  'giobjectinfo.c',
  'gipropertyinfo.c',
  'giregisteredtypeinfo.c',
  'girepository.c',
  'girffi.c',
  'gisignalinfo.c',
  'gistructinfo.c',
  'gitypeinfo.c',
  'gitypelib.c',
  'giunioninfo.c',
  'giunresolvedinfo.c',
  'givalueinfo.c',
  'givfuncinfo.c',
)

if cc.get_id() != 'msvc'
  custom_c_args = cc.get_supported_arguments([
    '-Wno-unused-parameter',
    '-Wno-duplicated-branches',
    '-Wno-type-limits',
    '-Wno-cast-align',
    '-Wno-missing-field-initializers',
  ])
endif

libgirepository = shared_library('girepository-2.0',
  sources: girepo_sources + girepo_ffi_sources + [gi_visibility_h],
  include_directories: [configinc, girepoinc],
  c_args: gir_c_args,
  version: library_version,
  soversion: soversion,
  darwin_versions: darwin_versions,
  gnu_symbol_visibility: 'hidden',
  link_args: glib_link_flags,
  dependencies: [
    libglib_dep,
    libgobject_dep,
    libgmodule_dep,
    libgio_dep,
    libgirepository_internals_dep,
  ],
  install: true,
)

libgirepository_dep = declare_dependency(
  link_with: libgirepository,
  dependencies: [libglib_dep, libgobject_dep, libgio_dep, libgmodule_dep],
  sources: [gi_visibility_h],
  include_directories: [girepoinc],
)

executable('gi-dump-types',
  sources: 'gi-dump-types.c',
  dependencies: [
    libgirepository_dep,
    libgiounix_dep,
    libgiowin32_dep
  ],
)

pkgconfig_variables = [
  'gidatadir=${datadir}/gobject-introspection-1.0',
  'girdir=' + gir_dir_pc_prefix / 'gir-1.0',
  'typelibdir=${libdir}/girepository-1.0',
]

pkg.generate(libgirepository,
  name: 'girepository',
  version: glib_version,
  filebase: 'girepository-2.0',
  install_dir: glib_pkgconfigreldir,
  description: 'GObject Introspection repository parser',
  variables: pkgconfig_variables,
  libraries: [libglib_dep, libgobject_dep],
)

if enable_gir
  libgirepository_gir_sources = [
    gi_visibility_h,
    girepo_headers,
    girepo_sources,
  ]
  libgirepository_gir_packages = [ 'girepository-2.0' ]
  libgirepository_gir_args = [
    '-DGI_COMPILATION',
    '--symbol-prefix=gi',
    '--identifier-prefix=GI',
  ]

  girepository_gir = gnome.generate_gir(libgirepository,
    sources: libgirepository_gir_sources,
    namespace: 'GIRepository',
    nsversion: '3.0',
    identifier_prefix: 'GI',
    symbol_prefix: 'gi',
    export_packages: libgirepository_gir_packages,
    header: 'girepository/girepository.h',
    includes: [ glib_gir[0], gmodule_gir[0], gobject_gir[0], gio_gir[0] ],
    install: true,
    dependencies: [ libglib_dep, libgobject_dep, libgmodule_dep, libgio_dep ],
    extra_args: gir_args + libgirepository_gir_args,
  )
endif

if build_tests
  subdir('tests')
endif
